﻿using System;

namespace Deferred.Library.Configuration
{
    /// <summary>
    /// 
    /// </summary>
    public class OptionCommand
    {
        private Option option;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="option"></param>
        public OptionCommand(Option option)
        {
            this.option = option;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="arguements"></param>
        /// <returns></returns>
        public string CommandCallback(string[] arguements)
        {
            bool value;

            if (2 != arguements.Length) return "Invalid number of arguements to setFPS";
            if (!Boolean.TryParse(arguements[1], out value)) return "Parameter was not a boolean";
            option.Value = value;

            return null;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="options"></param>
        /// <param name="commandLine"></param>
        /// <param name="fullName"></param>
        /// <param name="defaultValue"></param>
        /// <param name="callback"></param>
        static public void AddOptionCommand(IOptions options, ICommandLineEngine commandLine, string fullName, bool defaultValue, OptionCallback callback)
        {
            Option option = new Option(fullName, defaultValue, callback);
            options.AddOption(option);
            OptionCommand command = new OptionCommand(option);
            commandLine.AddCommand("set" + option.Name, "Toggles the "+ option.FullName +" option", command.CommandCallback);
        }
    }
}
